node{
    def releaseTag = '1.0'
    def serviceName = "${projectName}"
    def registryHost = '106.14.141.132:80'
    def gradle = "${gradleHome}/bin/gradle"

    stage('clean workspace') {
        cleanWs()
    }
    stage('git clone common') {
        sh 'mkdir common'
        dir('./common') {
            git credentialsId: 'gitee_credential', url: 'https://git.oschina.net/sidecar/common.git'
        }
    }
    stage("git clone infrastructure") {
        sh "mkdir infrastructure"
        dir("./infrastructure"){
            git credentialsId: 'gitee_credential', url: "https://gitee.com/sidecar/infrastructure.git"
        }
    }
    stage('Gradle build') {
        dir("./infrastructure/${serviceName}") {
            sh 'echo $GRADLE_HOME   , $PATH'
            sh "${gradle} clean buildDocker -x test"
        }
    }
    stage('clean images on server') {
        sh """
            imgs=`sudo docker images|grep '<none>'|awk '{print \$3}'`
            echo 'imgs value is:::::'\$imgs;
            if [ -n "\$imgs" ];
                then
                    for i in \$imgs;
                    do
                        sudo docker rmi -f \$i
                    done
            else
                echo 'there are no images to clean. '
            fi;
        """
    }
    stage('push images') {
        sh """
            sudo docker tag sidecar/${serviceName}:${releaseTag} ${registryHost}/sidecar/server-${serviceName}:${BUILD_NUMBER}
            sudo docker tag sidecar/${serviceName}:${releaseTag} ${registryHost}/sidecar/server-${serviceName}
            sudo docker push ${registryHost}/sidecar/server-${serviceName}:${BUILD_NUMBER}
            sudo docker push ${registryHost}/sidecar/server-${serviceName}
        """
    }
    stage('Publish build info') {
        println 'buid image success. '
    }
}